import { _decorator, Button, Component, Enum, Input, input, instantiate, Node, Prefab, Sprite, find, EventMouse } from 'cc';
import { PlantState, PlantType } from './enums';
import { MouseManager } from './MouseManager';
const { ccclass, property } = _decorator;

@ccclass('card_base')
export class card_base extends Component {
    
    @property
    private needPoint: number = 0;
    @property(Node)
    private lightNode: Node = null;
    @property(Node)
    private grayNode:Node = null;
    @property(Node)
    private bkNode:Node = null;
    @property
    private coolingTime:number = 0;
    private m_tmpCoolingTime : number = 0;
    @property({type: Enum(PlantState), displayName: "植物状态"})
    private m_plantState :PlantState = PlantState.DISABLE;

    @property({type:Enum(PlantType), displayName: "植物类型"})
    private m_plantType :PlantType = PlantType.SUNFLOWER;

    @property(Prefab)
    private m_currentPrefab: Prefab = null;


    start() {
        this.m_tmpCoolingTime = this.coolingTime;
        this.m_plantState = PlantState.DISABLE;
    }

    private handleStateChange():void
    {
        if (MouseManager.getInstance().getTotalPoint() >= this.needPoint) {
            this.m_plantState = PlantState.ENABLE;
            this.lightNode.active = true;
            this.grayNode.active = false;
            this.bkNode.active = false;
            //console.log("----冷却完成--\n", this.m_plantState);
        }
        else {
            this.m_plantState = PlantState.DISABLE;
            this.lightNode.active = false;
            this.grayNode.active = true;
            this.bkNode.active = true;
            //console.log("----冷却完成2--\n", this.m_plantState);
        }
    }

    update(deltaTime: number) {

        if(this.m_plantState == PlantState.COOLING)
        {
            this.m_tmpCoolingTime -= deltaTime;
            this.bkNode.getComponent(Sprite).fillRange = this.m_tmpCoolingTime/this.coolingTime;
            if(this.m_tmpCoolingTime <= 0)
            {
                 this.m_tmpCoolingTime = this.coolingTime;
                 this.handleStateChange();
            }
        }
        else
        {
            this.handleStateChange();
        }
    }

    public playCoolingAnimation(): void
    {
        this.m_plantState = PlantState.COOLING;
        this.lightNode.active = false;
        this.grayNode.active = true;
        this.bkNode.active = true;
    }

    public isEnable(): boolean
    {
        console.log('isEnable:', this.m_plantState == PlantState.ENABLE);
        return this.m_plantState == PlantState.ENABLE;
    }

    public getCurrentPrefab(): Prefab
    {
        return this.m_currentPrefab;
    }

    public getNeedPoint(): number
    {
        return this.needPoint;
    }
}



